package com.bw.classfiy.mvvm.viewmodel;

import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.LogUtils;
import com.bw.classfiy.R;
import com.bw.classfiy.mvvm.adapter.ClassFiyRigthAdapter;
import com.bw.classfiy.mvvm.adapter.ClassFiyTableAdapter;
import com.bw.classfiy.mvvm.model.ClassFiyModel;
import com.bw.mvvm.entity.ClassFiyEntity;
import com.bw.mvvm.entity.ClassFiyTableEntity;
import com.bw.classfiy.mvvm.view.ParticularsActivity;
import com.bw.mvvm.entity.BaseEntity;
import com.bw.mvvm.fleld.UIField;
import com.bw.mvvm.mvvm.viewmodel.BaseViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class ClassFiyViewModel extends BaseViewModel<ClassFiyModel> implements OnItemChildClickListener {

    public BaseQuickAdapter leftadapter = new ClassFiyTableAdapter();

    public BaseQuickAdapter rigthadapter = new ClassFiyRigthAdapter();

    public List<String> str = new ArrayList<>();

    public List<ClassFiyTableEntity.DataBean> entities = new ArrayList<>();

    public List<ClassFiyEntity.DataBean> dataBeans = new ArrayList<>();

    @Override
    protected void result(BaseEntity entity) {

        LogUtils.e(entity.getMessage());
        if (entity instanceof ClassFiyTableEntity){
            ClassFiyTableEntity entity1 = (ClassFiyTableEntity) entity;
            leftadapter.setNewInstance(entity1.getData());
            entities = entity1.getData();
        }else if (entity instanceof ClassFiyEntity){
            ClassFiyEntity entity1 = (ClassFiyEntity) entity;
            dataBeans = entity1.getData();
            rigthadapter.setNewInstance(entity1.getData());
            for (ClassFiyEntity.DataBean datum : entity1.getData()) {
                str.add(datum.getCategory_icon());
            }
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        HashMap<String,Object> map = new HashMap<>();
        map.put("parentId",0);
        RequestBody body = m.createBody(map);
        m.requestTable(body).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
        leftadapter.setOnItemChildClickListener( this);
        rigthadapter.setOnItemChildClickListener(this);
    }


    @Override
    public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter, @NonNull @NotNull View view, int position) {

        if (view.getId() == R.id.classfiy_values_imag) {
            EventBus.getDefault().postSticky(dataBeans.get(position));
            HashMap<String,Object> map = new HashMap<>();
            map.put(UIField.PAGECS, ParticularsActivity.class);
            startPage(map);

        } else if (view.getId() == R.id.classfiy_table_tv) {
            entities.get(position).setFlag(true);
            for (int j = 0; j < entities.size(); j++) {
                if (j != position)
                    entities.get(j).setFlag(false);
            }
            HashMap<String,Object> map = new HashMap<>();
            if (position >= 2){
                map.put("parentId",2);
                RequestBody body = m.createBody(map);
                m.requestValue(body).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(ClassFiyViewModel.this);
            }else if (position< 2){
                map.put("parentId",1);
                RequestBody body = m.createBody(map);
                m.requestValue(body).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(ClassFiyViewModel.this);
            }
            adapter.notifyDataSetChanged();
        }
    }
}
